/*
 * @Description: 抽奖信息 Store
 * @Author: xuyang
 * @Date: 2019-06-09 15:06:14 
 * @Last Modified by: XuYang
 * @Last Modified time: 2019-06-17 13:44:29
 */

import { observable, action, computed, runInAction } from 'mobx';
import {
    getLuckDataApi,
    getLuckResultApi,
    getResultListApi
} from '../api/luck';

export default class Login {
    constructor(rootStore) {
        this.rootStore = rootStore;
    }
    @observable intervalInfo = []; //抽奖信息 
    @observable prize = [];//奖品
    @observable luckReslut = {};//中奖的奖品
    @observable resultList = [];//中奖记录
    @observable banlace = '';//剩余奖池

    @observable page = 1;
    @observable total = 0;
    @observable last_page = 1;
    @observable pageSize = 10;

    @observable candyinfo = {
        candy:0,
        bonus: 0
    }
    @action setIntervalInfo(intervalInfo){
        this.intervalInfo = intervalInfo;
    }
    @action setPrize (prize){
        prize.splice(3,0,{
            id: 9,
            prize:'开始抽奖'
        });
        prize.splice(0,0,{
            id: 8,
            prize:'再接再厉',
            num: '0'
        });
        this.prize = prize;
    }
    @action setLuckResult (luckReslut){
        this.luckReslut = luckReslut;
    }
    @action addResultList (resultList){ 
        this.resultList = [...this.resultList ,...resultList.data];
    }
    @action setResultList (resultList){
        this.resultList = resultList.data;
    }
    @action setPage (page,total,last_page,pageSize){
        this.page = page;
        this.total = total;
        this.last_page = last_page;
        this.pageSize = pageSize;
    }
    @action setBanlace (banlace){
        this.banlace = banlace;
    }
    @action setCandyInfo (info){
        this.candyinfo = info;
    }
    /**
     * 获取抽奖信息
     */
    @action getLuckData = async () => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getLuckDataApi(ret);
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setPrize(response.data.prize);
                            this.setIntervalInfo(response.data.interval);
                            this.setBanlace(response.data.banlace);
                            this.setCandyInfo(response.data.candyinfo);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 中奖奖品
     */
    @action getLuckResult = async (cb) =>{
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getLuckResultApi(ret);
                    
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setLuckResult(response.data); 
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    }else {
                        // this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                    cb && cb(response);
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action getResultList = async (page) => {
        try { 
            page = page || {page: 1};
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getResultListApi(ret,page);
                    if (response.status === 10000) {
                        runInAction(() => {
                            if(page.page !==  this.page && page.page !== 1){
                                this.addResultList(response.data)
                            }else {
                                this.setResultList(response.data);
                            }
                            this.setPage(response.data.current_page,response.data.total,response.data.last_page,response.data.current_page);
                        });
                    }else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
}